package com.coolingme.logger;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;

/**
 * 将logger封装为json格式的装饰器
 *
 * @author wangyue
 * @date 2020/9/16 9:35
 */
public class JsonLogger extends BaseLoggerDecorator {
    public JsonLogger(Logger logger) {
        super(logger);
    }


    @Override
    public void info(String msg) {
        JSONObject json = newJsonObject();
        json.put("type", "info");
        json.put("message", msg);
        logger.info(json.toString());
    }

    @Override
    public void error(String msg) {
        JSONObject json = newJsonObject();
        json.put("type", "error");
        json.put("message", msg);
        logger.info(json.toString());
    }

    /**
     * 重载error
     *
     * @param e Exception
     */
    public void error(Exception e) {
        JSONObject json = newJsonObject();
        json.put("type", "error");
        json.put("exception", e.getMessage());
        logger.error(json.toString());
    }

    private JSONObject newJsonObject() {
        return new JSONObject();
    }

}
